//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_comment_api_serv

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/issue_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_entry_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/requirement_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/testcase_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_comment_api"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_watch_api"
)

type ProjectCommentApiImpl struct {
	project_comment_api.UnimplementedProjectCommentApiServer
}

func (apiImpl *ProjectCommentApiImpl) checkTarget(ctx context.Context, projectId string, targetType project_comment_api.COMMENT_TARGET_TYPE, targetId string) (bool, error) {
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_ENTRY {
		return project_entry_dao.EntryDao.Exist(ctx, targetId, projectId)
	}
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_REQUIRE_MENT {
		return requirement_dao.RequirementInfoDao.Exist(ctx, projectId, targetId)
	}
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_TASK || targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_BUG {
		return issue_dao.IssueDao.Exist(ctx, projectId, targetId)
	}
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_TEST_CASE {
		return testcase_dao.CaseInfoDao.Exist(ctx, targetId, projectId)
	}
	return false, nil
}

func (apiImpl *ProjectCommentApiImpl) convertWatchTargetType(targetType project_comment_api.COMMENT_TARGET_TYPE) project_watch_api.WATCH_TARGET_TYPE {
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_ENTRY {
		return project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_ENTRY
	}
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_REQUIRE_MENT {
		return project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_REQUIRE_MENT
	}
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_TASK {
		return project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_TASK
	}
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_BUG {
		return project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_BUG
	}
	if targetType == project_comment_api.COMMENT_TARGET_TYPE_COMMENT_TARGET_TEST_CASE {
		return project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_TEST_CASE
	}
	//never here
	return project_watch_api.WATCH_TARGET_TYPE_WATCH_TARGET_ENTRY
}
